<?php
namespace app\admin\controller;
use think\Controller;
use captcha;
use think\Session;
class Open extends Controller
{
    public function login()
    {
        header("Content-type: text/html; charset=utf-8");
        if($_POST){
            $p = $_POST;
            if(!captcha_check($p['code'])){
                return json(array(
                    'status'=>40000,
                    'msg'=>"验证码错误",
                    'refresh'=>0,
                    'tips'=>1,
                    'fun'=>'CodeRefresh()'
                ));
            }
            $info = db('admin_user')->field('id,username,password')->where('username',$p['username'])->find();
            if($info){
                $s = md5Check($p['password'],$info['password']);
                if($s){
                    Session::set('ADMIN_ID',$info['id']);
                    $admin_id = Session::get('ADMIN_ID');
                    return json(array(
                        'status'=>200,
                        'msg'=>"登录成功",
                        'url'=>url('Index/index'),
                    ));
                }else{
                    return json(array(
                        'status'=>40001,
                        'msg'=>"密码错误",
                        'refresh'=>0,
                        'tips'=>1,
                        'fun'=>'CodeRefresh()'
                    ));
                }
            }else{
                return json(array(
                    'status'=>40002,
                    'msg'=>"用户名不存在",
                    'refresh'=>0,
                    'tips'=>1,
                    'fun'=>'CodeRefresh()'
                ));
            }
        }else{
            $admin_id = Session::get('ADMIN_ID');
            if($admin_id){
                $this->redirect('Index/index');
            }else{
                return $this->fetch('login');
            }
        }
    }
}